<?php session_start(); ?>
    <input type="hidden" value="save" name="action"/>
    <input type="hidden" value="task" name="page"/>
    <input type="hidden" name="typeId" value="<?php echo  isset($updatedTask)?$updatedTask->typeId:$taskType->id; ?>"/>
    <?php

    //die("<br>".$updatedTask->points);
    echo isset($updatedTask)?"<input type='hidden' name='taskId' value='{$updatedTask->id}'/>":"";
    ?>
    <table cellspacing="10px" style="margin: 50px;margin-bottom: 10px">
        <tr>
            <td>
                Task Name
            </td>
            <td>
                <input type="text" name="taskName" id="taskName" class="validate[required,length[0,50]]" value="<?php echo $updatedTask->name; ?>"/>
            </td>
        </tr>
        <tr>
            <td>
                Task Creator
            </td>
            <td>
                <?php echo $_SESSION['CURRENT_USER']->firstName." ".$_SESSION['CURRENT_USER']->lastName; ?>
            </td>
        </tr>
        <tr>
            <td>
                Task Start Date
            </td>
            <td>
                <input type="text" name="startDate" id="startDate"class="validate[required]" value="<?php echo $updatedTask->startDate; ?>"/>
            </td>
        </tr>
        <tr>
            <td>
                Task End Date
            </td>
            <td>
                <input type="text" name="endDate" id="endDate"class="validate[required]" value="<?php echo $updatedTask->endDate; ?>"/>
            </td>
        </tr>
        <tr>
            <td>
                Task Status
            </td>
            <td>
                <select name="taskStatusId" id="taskStatusId">
                    <?php foreach($taskStatuses as $taskStatus){ ?>
                    <option value="<?php echo $taskStatus->id?>" <?php if($taskStatus->id == $updatedTask->taskStatusId) echo 'selected';elseif($taskStatus->id==1 && !isset($updatedTask)) echo 'selected'; ?> > <?php echo $taskStatus->name; ?></option>
                    <?php } ?>
                </select>
                <a onclick="updateTaskStatus('<?php echo $updatedTask->id; ?>',$('#taskStatusId').val());">Update</a>
                <div id="taskStatusDiv" style="color: blue"></div>
            </td>
        </tr>
        <tr>
            <td>
                Task Evaluation
            </td>
            <td>
                <select name="evalId">
                    <?php foreach($taskEvals as $eval){ ?>
                    <option value="<?php echo $eval->id?>" <?php if($eval->id == $updatedTask->evaluationId) echo 'selected';elseif($eval->id==4 && !isset($updatedTask)) echo 'selected'; ?> > <?php echo $eval->name; ?></option>
                    <?php } ?>
                </select>
            </td>
        </tr>
        <tr>
            <td>
                Task Type
            </td>
            <td>
                <?php 
                if(isset($updatedTask))
                echo $updatedTask->taskType;
                else
                echo $taskType->name;
                ?>
            </td>
        </tr>
        <tr>
            <td>
                Task Points
            </td>
            <td>
                <?php
                $points = 0;
                if(isset($updatedTask))
                $points = $updatedTask->points;
                else
                $points = $taskType->defaultPoints;
                ?>
                <input type="text" name="taskPoints" id="taskPoints" class="validate[required,custom[integer]]" value="<?php echo $points; ?>" />
            </td>
        </tr>
        <tr>
            <td>
                Task Description
            </td>
            <td>
                <textarea cols="80" id="description" name="description" rows="10"><?php echo ($updatedTask->description); ?></textarea>
            </td>
        </tr>
        <tr>
            <td>
                Assigned User to this task
            </td>
            <td></td>
        </tr>
            <?php
            foreach($users as $user){
            $selected=0;
            $evalValue=0;
            foreach($updatedTask->users as $assignedUser){
                if($user->id == $assignedUser->id){
                $selected=$assignedUser->id;
                $evalValue = $assignedUser->evalId;
                }
            }?>
                    <tr>
            <td></td>
            <td>
                
                       <?php if($selected!=0) {
                       if($user->id == $_SESSION["CURRENT_USER"]->id){;
                       ?>
                       <input type='checkbox' name='userIds[]' id='userIds' value='<?php echo $user->id ;?>' <?php if($selected>0)echo"checked"; ?> class="validate[minCheckbox[1]] checkbox"/> <?php echo $user->firstName." ".$user->lastName; ?>
                       <select id="eval_<?php echo $selected;?>" name="eval_<?php echo $selected;?>">
                        <?php foreach($taskEvals as $eval){ ?>
                        <option  value="<?php echo $eval->id?>" <?php if($eval->id ==$evalValue) echo 'selected';elseif($selected==0 && $eval->id==4)echo 'selected' ;?> > <?php echo $eval->name; ?></option>
                        <?php } ?>
                    </select>
                       <?php }}?>
                       <?php if($evalValue!=0){?><a style="cursor: pointer" onclick="updateUserEval('<?php echo $updatedTask->id;?>','<?php echo $user->id; ?>',$('#eval_<?php echo $selected;?>').val(),'eval_<?php echo $eval->id;?>');">update</a><?php }?>
                <div style="color: blue" id="eval_<?php echo $eval->id;?>"></div>
            </td></tr>
            <?php
            $selected=0;
            $evalValue=0;
            }
            ?>
        <tr>
            <td></td>
            <td></td>
        </tr>
    </table>